package com.coffee.admin.service.merchant.impl;

import com.coffee.admin.api.merchant.query.MerchantCityQuery;
import com.coffee.admin.api.merchant.req.MerchantCityReq;
import com.coffee.admin.api.merchant.result.MerchantCityResult;
import com.coffee.admin.api.merchant.result.MerchantShopResult;
import com.coffee.admin.api.merchant.service.MerchantCityService;
import com.coffee.admin.service.common.bean.GtRegion;
import com.coffee.admin.service.common.dao.GtRegionDao;
import com.coffee.admin.service.merchant.bean.MerchantCity;
import com.coffee.admin.service.merchant.dao.MerchantCityDao;
import com.coffee.core.entity.Page;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.enums.ExceptionEnum;
import com.coffee.core.enums.MerchantCityTypeEnum;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.util.BeanUtilExt;
import com.coffee.core.util.DTOUtils;
import com.coffee.core.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;


@DubboService(dynamic = true)
public class MerchantCityServiceImpl implements MerchantCityService {
    @Resource
    private MerchantCityDao merchantCityDao;
    @Resource
    private GtRegionDao gtRegionDao;

    @Override
    public Page<MerchantCityResult> getPager(MerchantCityQuery param, PagerInfo pr) {
        Page<MerchantCityResult> pager = new Page<MerchantCityResult>(pr.getStart(), pr.getPageSize());
        Integer count = merchantCityDao.count(param);
        pager.setTotalPage(count);
        if (count > 0) {
            List<MerchantCity> list = merchantCityDao.page(param, pr.getStart(), pr.getPageSize());
            pager.setData(DTOUtils.convertList(list, MerchantCityResult.class));
        }
        return pager;
    }

    @Override
    public Integer count(MerchantCityQuery param) {
        Integer count = merchantCityDao.count(param);
        return count;
    }

    @Override
    public MerchantCityResult getDetail(Integer id) {
        MerchantCity item = merchantCityDao.findById(id);
        if (item == null) {
            return null;
        }
        MerchantCityResult result = new MerchantCityResult();
        BeanUtilExt.copyProperties(result, item);

        return result;
    }

    @Override
    public List<MerchantCityResult> getList(MerchantCityQuery param, PagerInfo pr) {
        List<MerchantCity> list = merchantCityDao.page(param, pr.getStart(), pr.getPageSize());
        return DTOUtils.convertList(list, MerchantCityResult.class);
    }

    @Override
    public List<MerchantCityResult> getList(MerchantCityQuery param) {
        List<MerchantCity> list = merchantCityDao.page(param, 0, Integer.MAX_VALUE);
        List<MerchantCityResult> merchantCityResults = DTOUtils.convertList(list, MerchantCityResult.class);
        for (MerchantCityResult merchantCityResult : merchantCityResults) {
            if (merchantCityResult.getProvinceId() != null) {
                GtRegion province = gtRegionDao.findById(merchantCityResult.getProvinceId());
                if (province != null)
                    merchantCityResult.setProvinceName(province.getName());
            }
            if (merchantCityResult.getCityId() != null) {
                GtRegion city = gtRegionDao.findById(merchantCityResult.getCityId());
                if (city != null)
                    merchantCityResult.setCityName(city.getName());
            }
            if (merchantCityResult.getAreaId() != null) {
                GtRegion area = gtRegionDao.findById(merchantCityResult.getAreaId());
                if (area != null)
                    merchantCityResult.setAreaName(area.getName());
            }
        }
        return merchantCityResults;
    }

    @Override
    public List<MerchantShopResult> getShopList(MerchantCityQuery param) {
        List<MerchantCity> list = merchantCityDao.page(param, 0, Integer.MAX_VALUE);
        return DTOUtils.convertList(list, MerchantShopResult.class);
    }

    @Override
    public List<MerchantCityResult> getList() {
        MerchantCityQuery param = new MerchantCityQuery();
        List<MerchantCity> list = merchantCityDao.page(param, 0, Integer.MAX_VALUE);
        return DTOUtils.convertList(list, MerchantCityResult.class);
    }


    @Override
    public void delete(Integer id, Integer merchantId, String type) {
        merchantCityDao.deleteById(id, merchantId, type);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateIsDefault(Integer id, Integer merchantId, String isDefault) throws BusinessException {
        if ("Y".equals(isDefault)) {
            merchantCityDao.updateIsDefaultToN(merchantId);
            Integer integer = merchantCityDao.updateIsDefaultToY(id, merchantId);
            if (integer <= 0) {
                throw new BusinessException(ExceptionEnum.ERROR_0002.getCode(), ExceptionEnum.ERROR_0002.getMessage());
            }
        } else {
            merchantCityDao.updateIsDefaultToN(merchantId);
        }
    }

    @Override
    public void addOrUpdate(MerchantCityReq result) throws BusinessException {
        MerchantCity item = new MerchantCity();
        BeanUtilExt.copyProperties(item, result);
        if (StringUtils.isNoneBlank(result.getName()) && MerchantCityTypeEnum.SHOP.getCode().equals(result.getType())) {
            item.setReceiver(result.getName());
        }
        if (item.getId() == null) {
            item.setIsDefault("N");
            item.setType(result.getType());
            item.setCreateTime(new Date());
            merchantCityDao.insert(item);
        } else {
            MerchantCity tmp = merchantCityDao.findById(item.getId());
            if (tmp == null) {
                throw new BusinessException(ExceptionEnum.ERROR_0002.getCode(), ExceptionEnum.ERROR_0002.getMessage());
            }
            merchantCityDao.updateById(item);
        }
        if ("Y".equals(result.getIsDefault())){
            updateIsDefault(item.getId(),item.getMerchantId(),result.getIsDefault());
        }
    }

    @Override
    public Map<Integer, MerchantCityResult> getItemMap(Set<Integer> idSet) {
        Map<Integer, MerchantCityResult> map = new HashMap<Integer, MerchantCityResult>();
        List<MerchantCity> list = merchantCityDao.findByIds(StringUtil.setToList(idSet));
        List<MerchantCityResult> resultList = DTOUtils.convertList(list, MerchantCityResult.class);
        for (MerchantCityResult item : resultList) {
            map.put(item.getId(), item);
        }
        return map;
    }

    @Override
    public Map<Integer, MerchantCityResult> getItemMap() {
        Map<Integer, MerchantCityResult> map = new HashMap<Integer, MerchantCityResult>();
        MerchantCityQuery param = new MerchantCityQuery();
        List<MerchantCity> list = merchantCityDao.page(param, 0, Integer.MAX_VALUE);
        List<MerchantCityResult> resultList = DTOUtils.convertList(list, MerchantCityResult.class);
        for (MerchantCityResult item : resultList) {
            map.put(item.getId(), item);
        }
        return map;
    }


}
